package com.zx.baofu.alcohol.service.impl;

import com.zx.baofu.alcohol.mapper.SettingMapper;
import com.zx.baofu.alcohol.model.ActiveTime;
import com.zx.baofu.alcohol.model.DailyLimit;
import com.zx.baofu.alcohol.model.Result;
import com.zx.baofu.alcohol.model.ShareMsg;
import com.zx.baofu.alcohol.service.SettingService;
import com.zx.baofu.alcohol.util.MyStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SettingServiceImpl implements SettingService {
    @Autowired
    private SettingMapper settingMapper;

    @Override
    public ActiveTime getActiveTime() {
        ActiveTime at = settingMapper.getActiveTime();
        return at;
    }

    @Override
    public Result updateActiveTime(Integer id, String prizeName, Double prizeValue, Integer prizeNum, Integer totalNum
            , String start, String end, String manufacturer, String rule, String description, String prizeStart, String prizeEnd) {
        if (MyStringUtil.isNullorEmpty(start, end)){
            return new Result().fail("不能修改，因为活动时间设置错误");
        }
        if (MyStringUtil.isNullorEmpty(start, end)){
            return new Result().fail("不能修改，因为兑奖时间设置错误");
        }
        if (prizeNum > totalNum){
            return new Result().fail("不能修改，因为奖品总量不能小于每份奖品的数量");
        }
        int res = settingMapper.updateActiveTime(id, prizeName, prizeValue, prizeNum, totalNum, start, end, manufacturer, rule, description, prizeStart, prizeEnd);
        if (res <= 0){
            return new Result().fail("修改操作失败");
        }
        return new Result().success("修改成功");
    }

    @Override
    public DailyLimit getDailyLimit() {
        DailyLimit dl = settingMapper.getDailyLimit();
        return dl;
    }

    @Override
    public Result updateDailyLimit(Integer id, int limit) {
        int res = settingMapper.updateDailyLimit(id, limit);
        if (res <= 0){
            return new Result().fail("修改操作失败");
        }
        return new Result().success("修改成功");
    }

    @Override
    public ShareMsg getShareMsg() {
        ShareMsg sm = settingMapper.getShareMsg();
        return sm;
    }

    @Override
    public Result updateShareMsg(Integer id, String title, String img, String content) {
        int res = settingMapper.updateShareMsg(id, title, img, content);
        if (res <= 0){
            return new Result().fail("修改操作失败");
        }
        return new Result().success("修改成功");
    }
}
